import matplotlib.pyplot as plt
import numpy as np

# =============== 1. 数据 ===============
labels = ['总成本\n(million CNY)', '服务水平\n(%)', '缺货率\n(%)', '库存周转率\n(cycles)']
means  = np.array([7.9325, 95.45, 4.78, 4.23])
stds   = np.array([0.043,  0.29, 0.14, 0.14])

# 统一单位：成本→百万；百分比→绝对值
means[0] /= 1e6          # 元→百万
stds[0]  /= 1e6

# =============== 2. 绘图 ===============
fig, ax = plt.subplots(figsize=(7, 4))
bars = ax.bar(labels, means, yerr=stds, capsize=5,
              color=['#2E86AB', '#A23B72', '#F18F01', '#5D737E'],
              alpha=0.85, error_kw={'linewidth': 1.5, 'ecolor': 'black'})

# 数值标签
for bar, m, s in zip(bars, means, stds):
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2, height + s + 0.01*max(means),
            f'{m:.2f}±{s:.2f}', ha='center', va='bottom', fontsize=10)

# 图例与标题
ax.set_title('Q-learning 策略评估结果（均值 ± 1 标准差）', fontsize=14, pad=15)
ax.set_ylabel('数值', fontsize=12)
ax.grid(axis='y', alpha=0.3)
plt.tight_layout()

# 保存
plt.savefig('charts/评估指标对比图.png', dpi=300, bbox_inches='tight')
plt.show()